﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;

namespace SMS_Gateway
{
    public class BulkSMSGateway : HTTPGateway
    {
        protected override string Prefix
        {
            get { return "http://bulksms.vsms.net/eapi/submission/send_sms/2/2.0"; }
        }

        public override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode)
        {
            string prefix = Prefix;
            var usernameField = new KeyValuePair<string, string>("username", HttpUtility.UrlEncode(cred.Username));
            var passwordField = new KeyValuePair<string, string>("password", HttpUtility.UrlEncode(cred.Password));
            var recepientField = new KeyValuePair<string, string>("msisdn", "2"+cont.Number);
            var senderIdField = new KeyValuePair<string, string>("sender", _senderID);

            /*******************
             * Important : The routing group differs in prices in this service , I 
             * TODO: put that in the settings , somehow
            */
            var routingGroupField = new KeyValuePair<string, string>("routing_group", "2");

            KeyValuePair<string, string> messageField;
            KeyValuePair<string, string> dcaField;




            if (unicode)
            {
                string _hexencodedString = EncodeTo4DigitHex(sms.SMS_Body);


                messageField = new KeyValuePair<string, string>("message", _hexencodedString); // I read the manual
                dcaField = new KeyValuePair<string, string>("dca", "16bit");

            }
            else
            {

                messageField = new KeyValuePair<string, string>("message", HttpUtility.UrlEncode(sms.SMS_Body));
                dcaField = new KeyValuePair<string, string>("dca", "7bit");

            }


            string finalQuery = this.BuildQueryString(prefix, new KeyValuePair<string, string>[] { usernameField, passwordField,recepientField, messageField, dcaField, routingGroupField ,senderIdField});
            string result = this.HTTPSend(finalQuery);
            
            if (!result.StartsWith("HTTP Error") && result.StartsWith("0"))
            {
                return true;
            }
            else
            {
                return false;
            }

        }



        public BulkSMSGateway(string senderID):base(senderID)
        {

        }

        
    
    }
}
